package client.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

public class Board extends Composite {
	private Tile tiles[][];
	private int rows;
	private int columns;

	public Board(Composite parent, int style) {
		super(parent, style);  // Call Composite CTOR
	}
	
	public void initBoard(int rows, int columns, int tileStyle, MouseAdapter mouseAdapter) {
		//Dispose of old Tiles if present
		if (tiles != null) {
			for	(Tile tile[] : tiles) {
				for	(Tile t : tile) {
					t.dispose();
				}
		    }
		}
		
		this.rows = rows;
		this.columns = columns;
		
		tiles = new Tile[rows][columns];
		
		for(int i=0; i<rows; i++) {
			for (int j=0; j<columns; j++) {
				tiles[i][j] = new Tile(this, tileStyle);
				tiles[i][j].addMouseListener(mouseAdapter);
			}
		}
		
		GridLayout layout = new GridLayout(columns, true);
		layout.verticalSpacing = 0;
		layout.horizontalSpacing = 0;
		
		setLayout(layout);
	}
	
	public void updateBoard(int[][] boardData) {
		for(int i=0; i<rows; i++) {
			for (int j=0; j<columns; j++) {
				tiles[i][j].setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
				tiles[i][j].setValue(boardData[i][j]);
			}
		}
		
		layout();
	}
}
